package com.exam.baidu;


/**
 * date:
 * description:
 */
public class Solution102 {
    public static void main(String[] args) {
        int[] nums = new int[]{5, 4, 3, 2, 1};
        Solution102 instance = new Solution102();
        int[] res = instance.myQuickSort(nums);
        for (int num : res) {
            System.out.print(num + " ");
        }
    }

    public int[] myQuickSort(int[] nums) {
        quickSort(nums, 0, nums.length - 1);
        return nums;
    }

    public void quickSort(int[] nums, int left, int right) {
        if(left >= right) {
            return;
        }
        int i = left, j = right;
        while (i < j) {
            while(i < j && nums[j] >= nums[left]) {
                j--;
            }
            while(i < j && nums[i] <= nums[left]) {
                i++;
            }
            swap(nums, i, j);
        }
        swap(nums, i, left);
        quickSort(nums, left, i - 1);
        quickSort(nums, i + 1, right);
    }

    public void swap(int[] nums, int a, int b){
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }

}




